﻿using Org.BouncyCastle.Crypto;
using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IRepository.WareHouse;
using ProTrack.IServices.ReturnOrder;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.ReturnOrder
{
    public class OrderPoReturnServices : IOrderPoReturnServices
    {
        /// <summary>
        /// 采购退货
        /// </summary>
        /// <param name="db"></param>
        public OrderPoReturnServices(IOrderPoReturnRepository or,IBaseRepository db)
        {
            Or = or;
            Db = db;
        }
        public IBaseRepository Db { get; }
        public IOrderPoReturnRepository Or {  get; }

        public async Task<bool> AddAsync(OrderPoReturn model, List<OrderPoReturnItem> list)
        {
            var result = await Or.AddOrderPoReturnReceive(model, list);
            return result;
        }

        //获取所有
        public async Task<IEnumerable<OrderPoReturn>> GetAllAsync()
        {
            string sql = "selcet * from OrderPoReturn";
            var list = await Db.QueryAllAsync<OrderPoReturn>(sql);
            return list;
        }
        //分页 条件获取
        public async Task<IEnumerable<OrderPoReturn>> GetAsync(int PageIndex, int PageSize, string Number, string VendorId, string WarehouseId, string Status)
        {
            string sql = "select * from OrderPoReturn where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(VendorId))
            {
                sql += " and VendorId like @VendorId ";
            }
            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId ";
            }
            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status like @Status ";
            }
            var list = await Db.QueryAsync<OrderPoReturn>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", VendorId = $"%{VendorId}%", WarehouseId = $"%{WarehouseId}%", Status = $"%{Status}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string Number, string VendorId, string WarehouseId, string Status)
        {
            string sql = "select count(*) from OrderPoReturn where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(VendorId))
            {
                sql += " and VendorId like @VendorId ";
            }
            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId";
            }
            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status like @Status";
            }
            var list = await Db.GetRecoreCountAsync(sql, new { Number = $"%{Number}%", VendorId = $"%{VendorId}%", WarehouseId = $"%{WarehouseId}%", Status = $"%{Status}%" });
            return list;
        }
        public async Task<bool> DeleteAsync(int Id)
        {
            var result = await Or.DeleteOrderPoReturnReceive(Id);

            return result;
        }
        //修改
        public async Task<bool> UpdateAsync(OrderPoReturn model, List<OrderPoReturnItem> list)
        {
            var result = await Or.UpdateOrderPoReturnReceive(model, list);
            return result;
        }
        //批量删除
        public async Task<bool> DeleteAsync(int[] Ids)
        {
            var result = await Or.DeleteOrderPoReturnReceive(Ids);
            return result;
        }
       
       

        public async Task<IEnumerable<WhPoInbound>> GetAsyncWhPoInbound(int PageIndex, int PageSize, string Number, string VendorId, string WarehouseId, string Status)
        {
            string sql = "select * from WhPoInbound where WhPoInbound.id NOT IN (SELECT WhPoInboundId FROM OrderPoReturn) and 1=1 ";
            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status like @Status ";
            }
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(VendorId))
            {
                sql += " and VendorId like @VendorId ";
            }
            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId ";
            }
            
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";
            var list = await Db.QueryAsync<WhPoInbound>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", VendorId = $"%{VendorId}%", WarehouseId = $"%{WarehouseId}%", Status = $"%{Status}%" });
            return list;
        }
        public async Task<int> GetCountWhPoInbound(string Number, string VendorId, string WarehouseId, string Status)
        {
            string sql = "select count(*) from WhPoInbound where WhPoInbound.id NOT IN (SELECT WhPoInboundId FROM OrderPoReturn) and 1=1 ";
            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status like @Status ";
            }
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(VendorId))
            {
                sql += " and VendorId like @VendorId ";
            }
            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId ";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { Status = $"%{Status}%", Number = $"%{Number}%", VendorId = $"%{VendorId}%", WarehouseId = $"%{WarehouseId}%" });
            return count;
        }

        public async Task<IEnumerable<OrderPoReturnItemDto>> GetOrderPoReturnId(string OrderPoReturnId,string WarehouseId)
        {
            var list = await Db.QueryAsync<OrderPoReturnItemDto>(
                @"select item.Spec,item.ItemName,item.ItemCode,item.UnitOfMeasure,item.Model,op.*,(isk.StockCount-isk.SalesCount) as CurrCount
                 from OrderPoReturnItem op inner join Item item on op.ItemId=item.Id
                 inner join ItemStock isk on isk.ItemId=item.Id and isk.WareHouseId=@WareHouseId
                 where op.OrderPoReturnId = @OrderPoReturnId", new { OrderPoReturnId, WarehouseId });
            return list;
        }
    }
}
